package com.qs.questionnairesurveysystem.mapper;

import com.qs.questionnairesurveysystem.entity.Survey;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface SurveyMapper {

    // 创建新问卷
    @Insert("INSERT INTO survey (amount, quantity, unitPrice, totalDiscountPrice, title, `condition`, content, creatorId, participantCount, createTime, endTime, status, description,createType) " +
            "VALUES (#{amount}, #{quantity}, #{unitPrice}, #{totalDiscountPrice}, #{title}, #{condition}, #{content}, #{creatorId}, #{participantCount}, #{createTime}, #{endTime}, #{status}, #{description}, #{createType})")
    void insertSurvey(Survey survey);

    // 更新问卷信息
    @Update("UPDATE survey SET amount=#{amount}, quantity=#{quantity}, unitPrice=#{unitPrice}, totalDiscountPrice=#{totalDiscountPrice}, title=#{title}, " +
            "`condition`=#{condition}, content=#{content}, creatorId=#{creatorId}, participantCount=#{participantCount}, createTime=#{createTime}, endTime=#{endTime}, " +
            "status=#{status}, description=#{description}, createType=#{createType} WHERE surveyId=#{surveyId}")
    void updateSurvey(Survey survey);

    // 删除问卷
    @Delete("DELETE FROM survey WHERE surveyId=#{surveyId}")
    void deleteSurvey(Long surveyId);

    // 根据ID查询问卷
    @Select("SELECT * FROM survey WHERE surveyId=#{surveyId}")
    Survey getSurveyById(Long surveyId);

    // 查询所有问卷
    @Select("SELECT * FROM survey WHERE endTime > CURRENT_TIME()")
    List<Survey> getAllSurveys();

    // 根据创建者ID查询问卷
    @Select("SELECT * FROM survey WHERE creatorId=#{creatorId}")
    List<Survey> getSurveysByCreatorId(Long creatorId);

    @Select("SELECT * FROM survey WHERE endTime < CURRENT_TIME()")
    List<Survey> getEndSurveys();

    @Select("SELECT * FROM survey WHERE surveyId in (select surveyId from answers where respondentId = #{uid})")
    List<Survey> getEndSurveysRecord(Long uid);
}
